"""
@author: chy
@file: download.py
@time: 2023/12/4 17:08
"""
import requests
import time
import logging
def downloadtext(url):
    #
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
    }
    try:
        # try内部写有可能会出错的代码
        response = requests.get(url, headers=headers, verify=False)
        response.raise_for_status()
        # 设置编码格式
        response.encoding = response.apparent_encoding
        # 返回文本数据
        time.sleep(3)
        # print(url, "访问成功")
        logging.info(url+"访问成功")
        return response.text
    except:
        # 如果出错了会执行这里
        # print(url, "访问错误")
        logging.error(url+"访问错误")
        return ""


# 获取二进制
def downloadcontent(url):
    #
    headers = {
        "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0"
    }
    try:
        # try内部写有可能会出错的代码
        response = requests.get(url, headers=headers, verify=False)
        response.raise_for_status()
        # 设置编码格式
        response.encoding = response.apparent_encoding
        # 返回文本数据
        time.sleep(3)
        print(url, "访问成功")
        return response.content
    except:
        # 如果出错了会执行这里
        print(url, "访问错误")
        return ""